// pages/communicate/index/index.js
var app = getApp()
wx.cloud.init({
    env: 'cloud1-7gv4qn128a030d31'
  })
const db=wx.cloud.database()
const dbquestions = wx.cloud.database().collection("questions")
const dbarticle = wx.cloud.database().collection("article")
Page({
  data: {
    search:"",
    timeId:0,
    navbar: ['推荐', '问答', '好文'],
    currentTab: 0,
    questions:[],
    article:[],
    List:[],
   
  },
  onLoad(){
      var that=this
      that.onPullDownRefresh()
      this.onReady()
      that.getInfo()
  },
  onShow(){
    if (typeof this.getTabBar === 'function' &&  this.getTabBar()) {
        this.getTabBar().setData({
          selected: 1
        })
      }
  },
//   导航栏
  navbarTap: function(e){
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  },

// 搜索
async gosearch(e){
    const that=this
    console.log(e.detail.value);
    let search=e.detail.value
    await dbarticle.where({
        title:db.RegExp({
            regexp:search,
            options:'i'
        })
    }).get()
    .then(res=>{
        // console.log(res.data);
        that.setData({
            List:res.data,
            article:res.data
        })
    })
    await dbquestions.where({
        title:db.RegExp({
            regexp:search,
            options:'i'
        })
    }).get()
    .then(res=>{
        // console.log(res.data);
        that.setData({
            List:that.data.List.concat(res.data),
            questions:res.data,
            search:""
        })
        console.log(that.data.List);
    })
},
    //   获取文章和问答数据
    async getInfo(){
        var that=this
        // 随机获得文章
        await dbarticle.aggregate()
        .sample({
            size:50
        })
        .end()
        .then(res=>{
            that.setData({
                List:res.list
            })
            // console.log(that.data.List);
        })
        // 随机获得问题
        await dbquestions.aggregate()
        .sample({
            size:50
        })
        .end()
        .then(res=>{
            // console.log(res.list);
            // console.log(that.data.List);
            that.setData({
                List:that.data.List.concat(res.list)
            })
            // console.log(that.data.List);
        })
         // 打乱数组
         var l = that.data.List.length
         var index, temp
         while(l>0){
             index = Math.floor(Math.random()*l)
             temp = that.data.List[l-1]
             that.data.List[l-1] =  that.data.List[index]
             that.data.List[index] = temp
             l--
         }
         that.setData({
             List:that.data.List
         })
        //  console.log(that.data.List);
    },
//   下拉更新
  onPullDownRefresh(){
      var that=this;
      that.getInfo()
    dbquestions.get().then(res=>{
        // console.log(res.data);
        that.setData({
            questions:res.data
        })
        wx.stopPullDownRefresh()
    }),
    dbarticle.get().then(res=>{
        // console.log(res.data);
        that.setData({
            article:res.data
        })
        wx.stopPullDownRefresh()
    })
  }
})